package com.taru.wxhsh.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taru.wxhsh.common.JsonResult;
import com.taru.wxhsh.constant.JResponseConstants;
import com.taru.wxhsh.pojo.Detail;
import com.taru.wxhsh.pojo.Order;
import com.taru.wxhsh.service.OrderService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;


@Controller
public class OrderController {
    Logger logger  = LoggerFactory.getLogger(OrderController.class);
    @Autowired
    OrderService orderService;

    /**
     * 后台查询所有订单列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping("/order/list")
    public Object qryList(@RequestParam("pageNum") Integer pageNum,
                          @RequestParam("pageSize") Integer pageSize){
        JsonResult< PageInfo<Order>> result=null;
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<Order> orders = orderService.selectListWithUser();
            PageInfo<Order> pageInfo =new PageInfo<>(orders,pageSize);
            result = new JsonResult< PageInfo<Order>>(JResponseConstants.STATUS_OK,"查询订单成功",pageInfo);
        }catch (Exception ex){
            logger.error("查询订单列表异常:{}",ex.getMessage());
            result = new JsonResult< PageInfo<Order>>(JResponseConstants.STATUS_ERROR,"查询订单列表异常");
        }
        return result;
    }
    /**
     * 根据WX查询订单
     * @param pageNum
     * @param pageSize
     * @param uid
     * @return
     */
    @ResponseBody
    @RequestMapping("/order/find")
    public Object findById(@RequestParam("pageNum") Integer pageNum,
                           @RequestParam("pageSize") Integer pageSize, String uid){
        JsonResult<PageInfo> result=null;
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<Order> orders = orderService.selcetById(uid);
            PageInfo<Order> pageInfo =new PageInfo<>(orders,pageSize);
            result = new JsonResult<PageInfo>(JResponseConstants.STATUS_OK,"查询订单成功",pageInfo);
        }catch (Exception ex){
            logger.error("查询订单列表异常:{}",ex.getMessage());
            result = new JsonResult<>(JResponseConstants.STATUS_ERROR,"查询订单列表异常");
        }
        return result;
    }

    /**
     * 根据订单id查询订单详情
     * @param orderid
     * @return
     */
    @ResponseBody
    @RequestMapping("/order/detail")
    public Object qryDetail(String orderid){
        JsonResult<List<Detail>>  result = null;
        try {
            List<Detail> list = orderService.selectByOrderId(orderid);
            result = new JsonResult<List<Detail>>(JResponseConstants.STATUS_OK,"查询订单详情成功",list);
        }catch (Exception ex){
            logger.error("查询订单详情异常:{}",ex.getMessage());
            result = new JsonResult<List<Detail>>(JResponseConstants.STATUS_ERROR,"查询订单详情异常");
        }
        return result;
    }

    /**
     * 根据订单id查询单个订单信息
     * @param orderid
     * @return
     */
    @ResponseBody
    @RequestMapping("/order/qyrOderById")
    public Object qyrOderById(String orderid){
        JsonResult<Order> result = null;
        try {
            Order order = orderService.selectBy_OrderId(orderid);
            result = new JsonResult<>(JResponseConstants.STATUS_OK,"查询订单成功",order);
        }catch (Exception ex){
            logger.error("查询订单异常：{}",ex.getMessage());
            result = new JsonResult<>(JResponseConstants.STATUS_ERROR,"查询订单异常");
        }
        return result;
    }

    /**
     * 发货
     * @param orderid
     * @return
     */
    @ResponseBody
    @RequestMapping("/order/send")
    public Object send(String orderid){
        JsonResult<String> result = null;
        try {
            orderService.send(orderid);
            result = new JsonResult<>(JResponseConstants.STATUS_OK,"成功发货",orderid);
        }catch (Exception ex){
            logger.error("发货异常：{}",ex.getMessage());
            result = new JsonResult<>(JResponseConstants.STATUS_ERROR,"发货异常");
        }
        return  result;
    }

    /**
     * 作废
     * @param orderid
     * @return
     */
    @ResponseBody
    @RequestMapping("/order/invalid")
    public Object invalid(String orderid){
        JsonResult<String> result = null;
        try {
            orderService.invalid(orderid);
            result = new JsonResult<>(JResponseConstants.STATUS_OK,"作废成功");
        }catch (Exception ex){
            logger.error("作废异常：{}",ex.getMessage());
            result = new JsonResult<>(JResponseConstants.STATUS_ERROR,"作废异常");
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("/order/down")
    public Object IoDown(String files){
        return "";
    }

}
